/**
  ******************************************************************************
  * @file    User/user.c
  * @author  JUSHENG Application Team
  * @version V1.0.4
  * @date    12-14-2021
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2021 JUSHENG</center></h2>
  *
  *
  ******************************************************************************
  */ 

/* Includes ------------------------------------------------------------------*/
#include "include.h"
#include "user.h"
#include "tk_cfg.h"

/** @addtogroup Template_Project
  * @{
  */
u8 uart_rxbuf[16] = {0};
u8 rxdata;
u8 rx_cnt = 0;
u8 timeout_cnt = 0;
u8 rx_pointer;
u8 frameout_flag;
u8 tx_index;
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
void ust_uart_init(void)
{
    TYPE_LL_UST_UART_INIT ust_init;
    TYPE_LL_GPIO_INIT gpio_init;

    memset(&ust_init,0,sizeof(ust_init));
    memset(&gpio_init,0,sizeof(gpio_init));

    ll_gpio_pin_af_config(GPIOB,LL_GPIO_PIN_SOURCE_2,LL_GPIO_AF_1);
    ll_gpio_pin_af_config(GPIOB,LL_GPIO_PIN_SOURCE_3,LL_GPIO_AF_1);
    gpio_init.gpio_pin = LL_GPIO_PIN_2 | LL_GPIO_PIN_3;
    gpio_init.gpio_mode = LL_GPIO_MODE_AF;
    gpio_init.gpio_pupd = LL_GPIO_PUPD_UP;
    gpio_init.gpio_type = LL_GPIO_TYPE_OUT_PP;
    gpio_init.gpio_speed_level = LL_GPIO_SPEED_LEVEL_0;
    ll_gpio_init(GPIOB, &gpio_init);

    ust_init.wire_mode_choose = DISABLE; 
    ust_init.baudrate = LL_UST_UART_BAUDRATE_115200;
    ll_ust_uart_init(UST0, &ust_init);

    NVIC_InitTypeDef nvic;
    nvic.NVIC_IRQChannel = UST0_IRQn;
    nvic.NVIC_IRQChannelPriority = 0;
    nvic.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&nvic);
    ll_ust_uart_interrupt_enable(UST0);
    delay_us(1);
}

void ust_uart_handle(void)
{
    if(frameout_flag == 1)
    {
        ll_ust_uart_interrupt_disable(UST0);
        rx_pointer = rx_cnt;
        rx_cnt = 0;
        frameout_flag = 0;
        tx_index =0;
        if(0 != rx_pointer)
        {
            for(tx_index=0; tx_index<16; tx_index++)
            {
                ll_ust_uart_tx_data(UST0, uart_rxbuf[tx_index]);
            }
            ll_ust_uart_interrupt_enable(UST0);
        }
    }
}
/**
  * @}
  */

/*************************** (C) COPYRIGHT 2021 JUSHENG ***** END OF FILE *****/
